import { Article as Component } from '@/pagesComponents/Article';
import { env } from '@/shared/constants/env';
import { ArticleDto } from '@/shared/interfaces/generated';
import { generateMetaData } from '@/shared/lib/meta';
import { GetServerSideProps } from 'next';
import Head from 'next/head';

export default function Article({ id, data }: { id: string; data: ArticleDto }) {
  return (
    <>
      <Head>
        {generateMetaData({ title: data?.title })}
        <meta content={`app-id=1624893065, app-argument=try-companyName://articles/${id}`} name="apple-itunes-app" />
      </Head>
      <Component id={id} />
    </>
  );
}

export const getServerSideProps: GetServerSideProps = async (context) => {
  const id = context.params?.id;
  const res = await fetch(`${env.apiUrl}/v2/articles/${id}`);
  const data = await res.json();
  if (typeof id !== 'string' || data?.statusCode) {
    return {
      notFound: true,
    };
  }
  return {
    props: {
      id,
      data,
    },
  };
};
